package com.lk.basic.controller;

import com.lk.basic.service.IFastdfsService;
import com.lk.basic.util.AjaxResult;
import com.lk.basic.util.FastdfsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastdfs")
public class FastdfsController {

    @Autowired
    private IFastdfsService fastdfsService;

    @PostMapping
    public AjaxResult upload(MultipartFile file){
        try {
            byte[] bytes=file.getBytes();
            String originalFilename = file.getOriginalFilename();
            String extName = originalFilename.substring(originalFilename.indexOf(".") + 1);
            String path = fastdfsService.upload(bytes, extName);
            return AjaxResult.me().setResult(path);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @DeleteMapping
    public AjaxResult delete(String path) {
        try {
            fastdfsService.delete(path);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }


    }




}
